﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCSService
{
    public class PostService
    {
        /// <summary>
        /// Get list Category post by Parent ID
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<Post_Category> GetListCategoryPostByParent(int? parentID)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Post_Categories
                            where p.CategoryStatus==1
                            select p);

                if (parentID != null)
                {
                    item = item.Where(p => p.ParentID == parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentID == null);
                }

                return item.ToList();
            }
        }

        /// <summary>
        /// Get Category by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Post_Category GetCategory(int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Post_Categories
                            where p.CategoryID == id
                            select p).FirstOrDefault();
                return type;
            }
        }

        /// <summary>
        /// Đệ quy list ID danh mục tin bải
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public StringBuilder GetListCategoryByID(ref StringBuilder sb, int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Post_Categories
                            where p.ParentID == id
                            select p.CategoryID).ToList();
                foreach (var item in type)
                {
                    sb.Append(item + ",");
                    if (type.Count() > 0)
                    {
                        GetListCategoryByID(ref sb, item);
                    }
                }

                return sb;
            }
        }


        /// <summary>
        /// Get List Post By Category
        /// </summary>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public List<Post> GetListPostByCategory(int? cateID)
        {
            using (var data = new DataDataContext())
            {
                List<Post> listPost = new List<Post>();

                if (cateID != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListCategoryByID(ref sb, (int)cateID);
                    var arrId = sb.ToString();
                    listPost = (from p in data.Posts
                                   join m in data.Post_In_Categories on p.PostID equals m.PostID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Post_Categories on e0.CategoryID equals t.CategoryID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where p.PostStatus==1 && (e1.CategoryID == cateID || arrId.Substring(0, arrId.Length).Split(',').Contains(e1.CategoryID.ToString()))
                                   select p).ToList();
                }
                else
                {
                    listPost = (from p in data.Posts
                                join m in data.Post_In_Categories on p.PostID equals m.PostID into r0
                                from e0 in r0.DefaultIfEmpty()
                                join t in data.Post_Categories on e0.CategoryID equals t.CategoryID into r1
                                from e1 in r1.DefaultIfEmpty()
                                where p.PostStatus==1
                                select p).ToList();
                }

                return listPost;
            }
        }

        /// <summary>
        /// Get Post by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Post GetPost(int id)
        {
            using (var data = new DataDataContext())
            {
                var post = (from p in data.Posts
                               where p.PostID == id
                               select p).FirstOrDefault();
                return post;
            }
        }

    }
}
